//单车下拉菜单
import 'package:bruno/bruno.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:get/get_state_manager/src/simple/get_controllers.dart';
import 'package:get/get_state_manager/src/simple/get_view.dart';
import 'package:untitled/Page/local_map/custom_drawer.dart';
import 'package:untitled/dao/dart_api.dart';

class BikeInformationDropDownMenu
    extends GetView<BikeInformationDropDownMenuController> {
  final CustomDrawerController drawerController =
      Get.put(CustomDrawerController());

  @override
  Widget build(BuildContext context) {
    controller.getPlatformBikeData();
    return GetBuilder(
        init: BikeInformationDropDownMenuController(),
        builder: (BikeInformationDropDownMenuController ctx) => Obx(() {
              return drawerController.showLineDetail.value
                  ? SizedBox(
                      height: controller.getHeight(),
                      child: Padding(
                          padding: const EdgeInsets.fromLTRB(10, 20, 10, 5),
                          child: EasyRefresh(
                            bottomBouncing: false,
                            header: ClassicalHeader(
                              refreshText: "下拉加载更多",
                              refreshReadyText: "松开刷新",
                              refreshedText: "加载成功",
                              refreshFailedText: "加载失败",
                              refreshingText: "加载中...",
                              noMoreText: "没有更多了",
                            ),
                            footer: ClassicalFooter(
                              noMoreText: "没有更多了",
                              loadText: "加载更多",
                              loadingText: "加载中...",
                              loadedText: "加载成功",
                              loadReadyText: "松开加载",
                              loadFailedText: "加载失败",
                            ),
                            child: CustomScrollView(
                              slivers: [
                                SliverToBoxAdapter(
                                  child: Container(
                                    padding:
                                        const EdgeInsets.fromLTRB(10, 0, 0, 0),
                                    decoration: BoxDecoration(
                                        color: Colors.white,
                                        borderRadius: BorderRadius.circular(5)),
                                    child: Column(
                                      children: [
                                        Row(
                                          mainAxisAlignment:
                                              MainAxisAlignment.spaceBetween,
                                          children: [
                                            SizedBox(
                                                width: 260,
                                                child: Text(
                                                  // ignore: invalid_use_of_protected_member
                                                  controller.stationName.value,
                                                  style: const TextStyle(
                                                      fontSize: 22,
                                                      color: Colors.blue),
                                                  overflow:
                                                      TextOverflow.ellipsis,
                                                  // 显示省略号
                                                  maxLines: 1, // 最多显示一行
                                                )),
                                            IconButton(
                                                onPressed: () {
                                                  drawerController
                                                      .showLineDetail
                                                      .value = false;
                                                  controller.bikes.value = [];
                                                  controller.refreshBikes
                                                      .value = true;
                                                },
                                                icon: const Icon(
                                                  Icons.close,
                                                  size: 20,
                                                  color: Color.fromARGB(
                                                      255, 240, 240, 240),
                                                )),
                                          ],
                                        ),
                                        BrnLine(),
                                        Row(
                                          mainAxisAlignment:
                                              MainAxisAlignment.spaceBetween,
                                          children: [
                                            Align(
                                              alignment: Alignment.centerLeft,
                                              child: Row(
                                                mainAxisAlignment:
                                                    MainAxisAlignment.start,
                                                children: controller
                                                    .getPlatformTags(),
                                              ),
                                            ),
                                            Align(
                                              alignment: Alignment.centerRight,
                                              child: TextButton(
                                                  onPressed: () {},
                                                  child: Text(
                                                      "${controller.bikes.value.length}辆可用")),
                                            )
                                          ],
                                        ),
                                      ],
                                    ),
                                  ),
                                ),
                                // 刷新公交信息
                                SliverList(
                                  delegate: SliverChildBuilderDelegate(
                                    (context, index) {
                                      return bikeCard(
                                          controller.bikes.value[index]);
                                    },
                                    childCount: controller.bikes.value.length,
                                  ),
                                ),
                              ],
                            ),
                            onRefresh: () async {
                              controller.refreshBikes.value = true;
                              controller.getPlatformBikeData();
                            },
                          )),
                    )
                  : Container();
            }));
  }

  Widget bikeCard(bike) {
    return Padding(
      padding: const EdgeInsets.fromLTRB(0, 5, 0, 0),
      child: Container(
        padding: const EdgeInsets.fromLTRB(15, 10, 10, 10),
        decoration: const BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(Radius.circular(5))),
        width: 110,
        height: 160,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Expanded(
              flex: 3,
              child: Padding(
                  padding: const EdgeInsets.fromLTRB(0, 5, 0, 5),
                  child: Container(
                    alignment: Alignment.center,
                    width: 100,
                    height: 100,
                    child: Stack(
                      alignment: Alignment.bottomCenter,
                      children: [
                        Container(
                          width: 80,
                          height: 80,
                          decoration: const BoxDecoration(
                            color: Color.fromARGB(255, 240, 240, 240),
                            shape: BoxShape.circle,
                          ),
                          child: Padding(
                            padding: const EdgeInsets.all(12),
                            child: Image.asset(controller.bikeIconAsset),
                          ),
                        ),
                        Container(
                          padding: const EdgeInsets.fromLTRB(10, 5, 10, 5),
                          decoration: const BoxDecoration(
                              color: Colors.black45,
                              borderRadius:
                                  BorderRadius.all(Radius.circular(30))),
                          child: Text(
                            bike["bike_plateform"],
                            style: const TextStyle(
                              color: Colors.white,
                              fontSize: 12,
                            ),
                          ),
                        ),
                      ],
                    ),
                  )),
            ),
            Expanded(
                flex: 8,
                child: Container(
                  alignment: Alignment.center,
                  padding: const EdgeInsets.fromLTRB(0, 6, 0, 0),
                  child: BrnProgressBarChart(
                    padding: EdgeInsets.all(0),
                    barGroupSpace: 10,
                    barMaxValue: 100,
                    barChartStyle: BarChartStyle.horizontal,
                    xAxis: ChartAxis(
                        axisStyle: AxisStyle.axisStyleNone,
                        axisItemList: [
                          AxisItem(showText: ''),
                          AxisItem(showText: ''),
                          AxisItem(showText: '')
                        ]),
                    yAxis: ChartAxis(axisItemList: [
                      AxisItem(showText: ''),
                      AxisItem(showText: '')
                    ]),
                    // singleBarWidth: 50,
                    barBundleList: [
                      BrnProgressBarBundle(barList: [
                        BrnProgressBarItem(
                            text: '电量',
                            value: double.parse(bike['power']),
                            hintValue: 100),
                        BrnProgressBarItem(
                            text: '里程数',
                            value: (double.parse(bike['mileage']) * 100 / 80),
                            hintValue: 100)
                      ], colors: [
                        Color(0xff1545FD),
                        Color(0xff0984F9)
                      ]),
                    ],
                  ),
                )),
            Expanded(
                flex: 2,
                child: SizedBox(
                  height: 60,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        "${bike['power']}%",
                        style: const TextStyle(
                            fontSize: 15, color: Color(0xff0984F9)),
                      ),
                      Text(
                        "${bike['mileage']}km",
                        style: const TextStyle(
                            fontSize: 15, color: Color(0xff0984F9)),
                      ),
                    ],
                  ),
                ))
          ],
        ),
      ),
    );
  }
}

class BikeInformationDropDownMenuController extends GetxController {
  //设置数据
  String bikeIconAsset = "assets/img/icon/tool/icon_bike_selected.png";
  RxString stationName = "".obs;
  RxList bikes = [].obs; //单车信息集合
  RxBool refreshBikes = true.obs;

  @override
  void onInit() {
    super.onInit();
    getPlatformBikeData();
  }

  getPlatformBikeData() async {
    if (refreshBikes.value) {
      refreshBikes.value = false;
      var res = await DartAPI.findBikeByParkName(stationName);
      if (res != null) {
        bikes.value = res;
      }
      print('bikes= $bikes');
    }
  }

  // listEquals(bikes,newBikes){
  //   if (bikes.length != newBikes.length) {
  //     return false;
  //   }
  // // 逐个比较列表中的元素
  //   for (int i = 0; i < newBikes.length; i++) {
  //     // 如果列表中的对应位置的元素不相等，则列表不相等
  //     if (newBikes[i]['c_bikelat'] != bikes[i]['c_bikelat']) {
  //       return false;
  //     }
  //   }
  //   return true;
  // }
  getPlatformTags() {
    Set<String> tag = {};
    for (var i = 0; i < bikes.length; i++) {
      tag.add(bikes[i]["bike_plateform"]);
    }
    List<BrnTagCustom> tagList = [];
    var iterator = tag.iterator;
    while (iterator.moveNext()) {
      BrnTagCustom custom = BrnTagCustom(
          tagText: iterator.current,
          tagBorderRadius: BorderRadius.circular(15),
          textPadding: const EdgeInsets.all(5));
      tagList.add(custom);
    }
    return tagList;
  }

  getHeight() {
    if (bikes.length > 5) {
      return 640.0;
    } else {
      return (bikes.length + 1) * 200.0;
    }
  }
}
